package com.wzw.weibo;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class PicCompress {
	private int MIN_LENGTH = 100;
	private int WIDTH = 400;
	private int HEIGHT = 500;
	private String outPath = "D:";
	private boolean isOverlay = true;
	private float imageQuality = 70;
	
	private void OlddoThumbCompress(File file){
        
        BufferedImage bis=null;
        
        BufferedImage bid=null;
        
        try{

            int nw= 113; // 定义宽为113
            int nh= 84; //  定义高为84
            
            bis = ImageIO.read(file);
            int c_w= bis.getWidth();
            int c_h= bis.getHeight();

            if(c_w<114 && file.length()<MIN_LENGTH){        
                bis = null;
                return;
            }
            if(c_w>WIDTH){
                nw  = WIDTH;
                nh = c_h * (WIDTH)/c_w;
                c_h=nh;
                c_w=nw;
            }    
            
            if(c_h>HEIGHT){
                nh = HEIGHT;
                nw  = c_w * (HEIGHT)/c_h;             
            }
            
            
            
            bid= new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);                        
            Graphics g = bid.getGraphics();

            g.setColor(Color.white);

            g.fillRect(0,0, WIDTH, HEIGHT);
            
            g.drawImage(bis,(WIDTH-nw)/2,0,nw,nh,null);
            
            
            
            long oldFileSize=file.length();
            
             
            if(isOverlay){
                ImageIO.write(bid, "jpeg", file);
            } else{
                ImageIO.write(ImageIO.read(file), "jpeg", new File(outPath+File.separator+file.getName()));
            }
            long newFileSize=file.length();
            System.out.println("ok:"+file+"\t"+oldFileSize+"\t"+newFileSize);
        } catch (Exception e){
        	System.out.println(file+" error:"+e.getMessage());
             
        }
        
        
        bis=null;
        
        bid=null;
    }
	public void copyFile(String oldPathFile, String newPathFile) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPathFile);
            if (oldfile.exists()) {
            	
            	InputStream inStream = new FileInputStream(oldPathFile);
            	FileOutputStream fs = new FileOutputStream(newPathFile);
                byte[] buffer = new byte[1444];
                while((byteread = inStream.read(buffer)) != -1){
                	bytesum+= byteread;
                    //System.out.println(bytesum);
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
        }catch (Exception e) {
//        	e.printStackTrace();
        }
}
	public void doThumbCompress(String Path,String FName,boolean needcopytn){
		
		GetClassPath getclasspath = new GetClassPath();
		String rootPath = getclasspath.getClassPath();
		//String fullPath = rootPath+Path+FName;
		String fullPath = Path + FName;
//		System.out.println(fullPath);
		String newfullPath = Path+"tn/"+FName;
//		System.out.println(newfullPath);
		if(needcopytn)
		{
	        File dir =  new File(Path+"tn/");
	        if (!dir.exists()) dir.mkdirs();
		}
        File file = new File(fullPath);
		
		BufferedImage bis=null;
        
        BufferedImage bid=null;
        
        try{

            int nw= 113; // 定义宽为113
            int nh= 84; //  定义高为84
            
            bis = ImageIO.read(file);
            int c_w= bis.getWidth();
            int c_h= bis.getHeight();

            if(c_w<114 && file.length()<MIN_LENGTH){        
                bis = null;
                return;
            }
            if(c_w>WIDTH){
                nw  = WIDTH;
                nh = c_h * (WIDTH)/c_w;
                c_h=nh;
                c_w=nw;
            }    
            
            if(c_h>HEIGHT){
                nh = HEIGHT;
                nw  = c_w * (HEIGHT)/c_h;             
            }
            
            nw = c_w;
            nh = c_h;
            
            
            bid= new BufferedImage(nw, nh,BufferedImage.TYPE_INT_RGB);                        
            Graphics g = bid.getGraphics();

            g.setColor(Color.white);

            g.fillRect(0,0, WIDTH, HEIGHT);
            
            g.drawImage(bis,0,0,nw,nh,null);
            
            
            
            long oldFileSize=file.length();
            
             
            if(isOverlay){
                ImageIO.write(bid, "jpeg", file);
                if(needcopytn)
                {
                	copyFile(fullPath,newfullPath);
                }
            } else{
                ImageIO.write(ImageIO.read(file), "jpeg", file);
            }
            long newFileSize=file.length();
            System.out.println("ok:"+file+"\t"+oldFileSize+"\t"+newFileSize);
        } catch (Exception e){
        	System.out.println(file+" error:"+e.getMessage());
        }
        bis=null;
        bid=null;
    }
	
	public void doSimpleCompress(String path,String fileName, int compressPercent){
		
		String fullPath = path + fileName;
//		System.out.println(fullPath);
//		if(needcopytn)
//		{
//	        File dir =  new File(Path+"tn/");
//	        if (!dir.exists()) dir.mkdirs();
//		}
        File file = new File(fullPath);
		
		BufferedImage bis=null;
        BufferedImage bid=null;
        try{
            bis = ImageIO.read(file);
            int c_w= bis.getWidth();
            int c_h= bis.getHeight();

            int nw = c_w * compressPercent / 100;
            int nh = c_h * compressPercent / 100;
            
            bid= new BufferedImage(nw, nh,BufferedImage.TYPE_INT_RGB);                        
            Graphics g = bid.getGraphics();
            g.setColor(Color.white);
            g.fillRect(0,0, WIDTH, HEIGHT);
            g.drawImage(bis,0,0,nw,nh,null);
            
            long oldFileSize=file.length();
            ImageIO.write(bid, "jpeg", file);
             
            long newFileSize=file.length();
            System.out.println("ok:"+file+"\t"+oldFileSize+"\t"+newFileSize);
        } catch (Exception e){
        	System.out.println(file+" error:"+e.getMessage());
        }
        bis=null;
        bid=null;
    }
        
        
     //压缩图片代码�?
    /** *//**
     * 可以自定义压缩比�?
     * @param file
     */
    private void doCompressQuality(File file)  {
        BufferedImage src = null;
        FileOutputStream out =  null;
        JPEGImageEncoder encoder = null;
        JPEGEncodeParam  param   = null;
        
        try{
            src= ImageIO.read(file);//or png.
            if(isOverlay){
                out= new FileOutputStream(file);
            }else{
                out= new FileOutputStream(outPath+File.separator+file.getName());
            }
            encoder= JPEGCodec.createJPEGEncoder(out);
            param= encoder.getDefaultJPEGEncodeParam(src);            
            param.setQuality(imageQuality, false);
            encoder.setJPEGEncodeParam(param);            
            encoder.encode(src);//近JPEG编码
            out.close();
            //ImageIO.write(src, "jpeg", new File(outPath+File.separator+file.getName()));            
            System.out.println("over:"+file.getPath());
        } catch (IOException e){
        	System.out.println("compress error:"+file.getPath()+";error:"+e.getMessage());
        }
        out = null;
        encoder = null;
        param   = null;
        src     = null;
    }

    /** *//**
     * 压缩图片，默认压缩比�?5%
     * @param file
     */
    private void doCompress(File file){
        File descFile = null;
        boolean writeOk=false;
        long fileSize=file.length();

        try{
            
            if(isOverlay){                
                descFile = file;
                writeOk = ImageIO.write(ImageIO.read(file), "jpeg", descFile);
            }else{
                descFile = new File(outPath+File.separator+file.getName());
                writeOk = ImageIO.write(ImageIO.read(file), "jpeg", descFile);
            }        
            
            if(writeOk){
            	System.out.println("ok:"+file.getPath()+"\t"+fileSize+"\t"+descFile.length());
            }else{
            	System.out.println("fail:"+file.getPath()+"\t"+fileSize+"\t"+descFile.length());
            }            
            
        } catch (Exception e){
        	System.out.println("compress error:"+file.getPath()+";descFile="+descFile+";error:"+e.getMessage());
        }
        descFile=null;
    }
    public static void main(String[] args) {		
		try
		{
			PicCompress picCompress = new PicCompress();
//			File file = new File("c:/aaa.jpg");
//			System.out.println(file.exists());
			//picCompress.doThumbCompress("D:\\Tomcat-5.0.28\\webapps\\midaut\\upload\\","img05.png",true);
			picCompress.doSimpleCompress("D:\\Tomcat-5.0.28\\webapps\\midaut\\upload\\", "img05.png", 100);
			//picCompress.setHEIGHT(85);
			//picCompress.doThumbCompress("D:\\Tomcat-5.0.28\\webapps\\midaut\\upload\\","img05.png",false);
		}catch (Exception e)
		{
			e.printStackTrace();
			System.out.println("Test end1!");
		}
	}
	public int getHEIGHT() {
		return HEIGHT;
	}
	public void setHEIGHT(int height) {
		HEIGHT = height;
	}
	public int getWIDTH() {
		return WIDTH;
	}
	public void setWIDTH(int width) {
		WIDTH = width;
	}
}
